package com.sdry.web.controller.base;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.base.BaseController;
import com.sdry.entity.base.Staff;
import com.sdry.service.base.StaffService;
import com.sdry.utils.MD5;
/**
 * 
 * @ClassName:StaffController
 * @Description:员工
 * @Author tdd
 * @Date 2018年4月25日下午5:05:43
 * @version 1.0
 */
@Controller
@RequestMapping("/login")
public class LoginController extends BaseController{
	
	@Resource StaffService  staffService;
	/**
	 * 根据工号和密码查信息
	 * @param scode 工号
	 * @param pass 密码
	 * @throws IOException 
	 */
	@RequestMapping("/login")
	public void login(String scode,String pass,HttpSession session,HttpServletResponse response) throws IOException{
		if (null != scode && null != pass && !scode.equals("") && !pass.equals("") ) {
			Staff staff = new Staff();
			staff.setScode(scode);
			staff.setPass(pass);
			staff = staffService.queryByScodePass(staff);
			if(null != staff){
				session.setAttribute("staff", staff);
				response.getWriter().print(1);
			}else{
				response.getWriter().print(0);
			}
		}else{
			response.getWriter().print(0);
		}
	}

	
	/**
	 * 登录
	 * @throws IOException 
	 */
	@RequestMapping("/toLogin")
	public String toLogin(HttpSession session,HttpServletResponse response) throws IOException {
		 //从session中取出用户身份信息
        Staff staffSession = (Staff) session.getAttribute("staff");
		if(staffSession != null){
			session.invalidate(); 
		}
		return "/login";
	}
}
